package com.haiqi.modules.ship.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.dao.ManningInfoMapper;
import com.haiqi.common.persistence.entity.ManningInfo;
import com.haiqi.common.persistence.entity.ShipInfo;
import com.haiqi.modules.ship.service.ShipManningService;
import com.haiqi.modules.ship.service.ShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hythzx on 2016/5/26.
 */
@Controller
@RequestMapping("/manage/ship/manning")
@SafeAuthority(menuCode = "fleetManage,monitorLocation")
public class ShipManningController extends BaseController{

    @Autowired
    private ShipManningService service;

    @Autowired
    private ShipService shipService;

    @Autowired
    private ManningInfoMapper manningInfoMapper;

    private String menuCode = "fleetManage";

    @RequestMapping("/list")
    public String list(ManningInfo manningInfo, PageBean pageBean, HttpSession session, Model model){
        generateMenuCode(session, menuCode);
        PageInfo<ManningInfo> pageInfo = service.getDataList(manningInfo,pageBean);
        ShipInfo shipInfo = shipService.getShipInfo(manningInfo.getShipid());
        model.addAttribute("param",manningInfo);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("shipInfo",shipInfo);
        return "/modules/ship/manning/list";
    }


    @RequestMapping("/ajax/list")
    @ResponseBody
    public List<ManningInfo> getJsonList(ManningInfo manningInfo, HttpSession session){
        List<ManningInfo> shipInfoList = service.getDataList(manningInfo);
        return shipInfoList;
    }

    @RequestMapping("/detail")
    @ResponseBody
    public ManningInfo getShipInfo(ManningInfo manningInfo){
        manningInfo = service.getManningInfo(manningInfo.getId());
        return manningInfo;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map updateShipInfo(ManningInfo manningInfo, HttpSession session){
        Map<String,Boolean> response = new HashMap<>();
        boolean success = service.update(manningInfo);
        response.put("success",success);
        return response;
    }
    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(ManningInfo manningInfo){
        Map<String,Boolean> response = new HashMap<>();
        boolean success = service.delete(manningInfo.getId());
        response.put("success",success);
        return response;
    }

    @RequestMapping("/removeMobileAccount")
    @ResponseBody
    public Map removeMobileAccount(int id){
        Map<String,Boolean> response = new HashMap<>();
        int i = manningInfoMapper.removeAccount(id);
        if(i > 0){
            response.put("success",true);
        }else{
            response.put("success",false);
        }
        return response;
    }

    @RequestMapping("/setMobileAccount")
    @ResponseBody
    public Map setMobileAccount(int id,String userId){
        Map<String,Boolean> response = new HashMap<>();
        ManningInfo manningInfo = new ManningInfo();
        manningInfo.setId(id);
        manningInfo.setUserId(userId);
        int i = manningInfoMapper.updateByPrimaryKeySelective(manningInfo);
        if(i > 0){
            response.put("success",true);
        }else{
            response.put("success",false);
        }
        return response;
    }

}
